//Ford Fulkerson MaxFlow by BFS
//poj1273Drainage Ditches
//Author wcycgs
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 210
#define INF 10000010
using namespace std;

class Network {
private:
	int size;
	int prev[MAX];
	bool augmentable(int, int);
public:
	int cap[MAX][MAX];
	void clear(int);
	int maxFlow(int, int);
};
void Network::clear(int n) {
	memset(cap, 0, sizeof(cap));
	size = n;
}
bool Network::augmentable(int s, int t) {
	queue<int> queue;
	memset(prev, -1, sizeof(prev));
	prev[s] = s;
	queue.push(s);
	while(!queue.empty()) {
		int p = queue.front(), i;
		queue.pop();
		for(i = 0; i < size; i++) {
			if(cap[p][i] > 0 && prev[i] == -1) {
				prev[i] = p;
				if(i == t) {
					return true;
				} else {
					queue.push(i);
				}
			}
		}
	}
	return false;
}
int Network::maxFlow(int s, int t) {
	int flow = 0, i;
	while(augmentable(s, t)) {
		int extend = INF;
		for(i = t; i != s; i = prev[i]) {
			if(extend > cap[prev[i]][i]) {
				extend = cap[prev[i]][i];
			}
		}
		for(i = t; i != s; i = prev[i]) {
				cap[prev[i]][i] -= extend;
				cap[i][prev[i]] += extend;
		}
		flow += extend;
	}
	return flow;
}
int main()
{
	int v, e, a, b, k;
	Network ne;
	while (scanf("%d%d", &e, &v) != EOF) {
		ne.clear(v);
		for (int i = 0; i < e; ++i) {
			scanf("%d%d%d", &a, &b, &k);
			ne.cap[a - 1][b - 1] += k;
		}
		printf("%d\n", ne.maxFlow(0, v - 1));
	}
	return 0;
}

